package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 登录
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo login(UserInfo userInfo) {
        // 查询： select * from user_info where login_name = ? and passwd = ?
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        // 获取用户名
        userInfoQueryWrapper.eq("login_name",userInfo.getLoginName());
        // MD5加密
        String newPwd = DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes());
        // 获取加密的密码
        userInfoQueryWrapper.eq("passwd",newPwd);

        //查询数据库获取到返回结果
        UserInfo info = userInfoMapper.selectOne(userInfoQueryWrapper);
        //判断
        if (info != null) {
            return info;
        }
        //默认返回null
        return null;
    }
}
